package com.sf.rbac.web.controller;

import com.github.pagehelper.PageInfo;
import com.sf.rbac.anno.RequiredPermission;
import com.sf.rbac.domain.Department;
import com.sf.rbac.qo.QueryObject;
import com.sf.rbac.service.IDepartmentService;
import com.sf.rbac.util.JsonResult;
import com.sf.rbac.util.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "部门删除",expression = "department:delete")
    //@PathVariable   把路径参数设置到参数这里来
    public JsonResult delete(@PathVariable Long id) {
        departmentService.delete(id);
        return new JsonResult().success();
    }

    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "部门新增或编辑",expression = "department:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Department department) {
        departmentService.saveOrUpdate(department);
        return JsonResult.success();

    }

    @GetMapping("/info/{id}")
    @RequiredPermission(name = "查询单个部门",expression = "department:info")
    public JsonResult info(@PathVariable Long id) {
        Department department = departmentService.info(id);
        return JsonResult.success("操作成功", department);

    }

    @GetMapping("/listAll")
    @RequiredPermission(name = "查询所有部门",expression = "department:listAll")
    public JsonResult listAll() {
        List<Department> list = departmentService.listAll();
        return JsonResult.success("操作成功", list);

    }

    @GetMapping("/list")
    @RequiredPermission(name = "部门列表",expression = "department:list")
    public JsonResult list(QueryObject queryObject) {
        PageInfo<Department> pageInfo = departmentService.query(queryObject);
        PageResult result = new PageResult();
        BeanUtils.copyProperties(pageInfo, result);
        return JsonResult.success(result);

    }
}
